package com.wx.mall.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.sys.entity.Admin;
import com.wx.mall.sys.entity.UserRole;
import com.wx.mall.sys.service.IUserRoleService;
import com.wx.mall.sys.service.IUserService;
import com.wx.mall.sys.vo.req.UserPageReqVO;
import com.wx.mall.sys.vo.req.UserUpdateInsertReqVO;
import com.wx.mall.sys.vo.resp.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author j
 * @since 2020-03-17
 */
@RestController
@RequestMapping("/user")
@Api(tags = "成员模块相关接口")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRoleService userRoleService;

    @PostMapping("/users")
    @ApiOperation("分页获取规格数据")
    public DataResult pageInfo(@Valid UserPageReqVO vo) {

        PageVO page = userService.findPage(vo);
        return DataResult.success(page);
    }

    @DeleteMapping("/user/{id}")
    @ApiOperation("删除")
    public DataResult deleteById(@PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        boolean b = userService.removeById(id);
        if (!b) {
            throw new BusinessException(90001, "成员删除失败");
        }
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", id);
        boolean remove = userRoleService.remove(queryWrapper);
        if (!remove) {
            throw new BusinessException(90001, "成员删除失败");
        }
        return DataResult.success();
    }

    @PutMapping("/user")
    @ApiOperation("编辑接口")
    public DataResult updateById(@Valid UserUpdateInsertReqVO vo) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(vo, admin);
        boolean b = userService.updateById(admin);
        if (!b) {
            throw new BusinessException(90002, "成员编辑失败");
        }
        UserRole userRole = new UserRole();
        userRole.setRoleId(vo.getRoleId());
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", vo.getId());
        boolean b1 = userRoleService.update(userRole, queryWrapper);
        if (!b1) {
            throw new BusinessException(90003, "成员编辑失败");
        }
        return DataResult.success();
    }

    @PostMapping("/user")
    @ApiOperation("新增接口")
    public DataResult add(@Valid UserUpdateInsertReqVO vo) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(vo, admin);
        boolean save = userService.save(admin);
        if (!save) {
            throw new BusinessException(90004, "成员新增失败");
        }
        UserRole userRole = new UserRole();
        userRole.setRoleId(vo.getRoleId());
        userRole.setUserId(admin.getId());
        System.out.println(userRole);
        boolean save1 = userRoleService.save(userRole);
        if (!save1) {
            throw new BusinessException(90004, "成员新增失败");
        }
        return DataResult.success();
    }
}

